package alpha2;

import java.net.URLEncoder;
import java.nio.charset.Charset;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class Group extends Common{

	public static void get(Integer skip, Integer limit) throws Exception{
		System.out.println("Start GET /group 获取所有用户组 ...");
	        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet request = new HttpGet(HOST + "/group" + "?skip=" + skip + "&limit=" + limit);

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
	}
	
	public static void add(String tag) throws Exception {
        System.out.println("Start POST /group 创建用户组 ...");
        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost request = new HttpPost(HOST + "/group");
        
        MultipartEntity reqEntity = new MultipartEntity();  
        reqEntity.addPart("tag", new StringBody(tag,Charset.forName("UTF-8")));
        request.setEntity(reqEntity); 

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
    }
	
	public static void get(String groupId) throws Exception{
		System.out.println("Start GET /group/{group_id} 获取单个用户组 ...");
	        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet request = new HttpGet(HOST + "/group/" + groupId);

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
	}
	
	public static void update(String groupId, String tag) throws Exception {
        System.out.println("Start PUT /group/{group_id} 修改某个用户组 ...");
        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPut request = new HttpPut(HOST + "/group/" + groupId);
        
        MultipartEntity reqEntity = new MultipartEntity();  
        reqEntity.addPart("tag", new StringBody(tag,Charset.forName("UTF-8")));
        request.setEntity(reqEntity); 

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
    }
	
	public static void user(String groupId, String userId) throws Exception {
        System.out.println("Start POST /group/{group_id}/{user_id} 向用户组中加入用户 ...");
        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost request = new HttpPost(HOST + "/group/" + groupId + "/" + userId);

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
    }
	
	public static void delete(String groupId, String userId) throws Exception{
		System.out.println("Start DELETE /group/{group_id}/{user_id} 删除用户组中的用户 ...");
	        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpDelete request = new HttpDelete(HOST + "/group/" + groupId + "/" + userId);

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
        
	}
	
	@SuppressWarnings("deprecation")
	public static void get(String query, Integer skip, Integer limit) throws Exception{
		System.out.println("Start GET /group_query 查询用户组 ...");
	        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet request = new HttpGet(HOST + "/group_query" + "?query=" + URLEncoder.encode(query) + "&skip=" + skip + "&limit=" + limit);

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
	}
	
	public static void delete(String groupId) throws Exception{
		System.out.println("Start DELETE /group/{group_id} 删除某个用户组 ...");
	        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpDelete request = new HttpDelete(HOST + "/group/" + groupId);

        //发起网络请求
        httpclient.execute(request);
        
	}
	
}
